﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebBanThuoc.DTO;
using System.Data.SqlClient;

namespace WebBanThuoc.DAO
{
    public class SanPhamXem_DAO
    {
        private SqlConnection connection;
        private SqlConnection connectionTemp;
        //public bool hasData { set; get; }
        public SanPhamXem_DAO(SqlConnection connection, SqlConnection connectionTemp)
        {
            this.connection = connection;
            this.connectionTemp = connectionTemp;
        }
        private List<SanPhamXem> getListBy(string whereCom, string orderCom)
        {

            List<SanPhamXem> data = new List<SanPhamXem>();
            string comStr = "Select * from [SanPham]";
            if (whereCom.Trim() != "")
                comStr += " where " + whereCom;
            if (orderCom.Trim() != "")
            {
                comStr += " order by " + orderCom;
            }

            try
            {
                
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, comStr);

                HinhAnh_DAO daoHH = new HinhAnh_DAO(connectionTemp);
                //this.hasData = dap.HasData;
                if (dap.HasData)
                {
                    while (dap.DaReader.Read())
                    {
                        SanPhamXem objTemp = new SanPhamXem();
                        SanPham objSP = new SanPham();

                        objSP.MaSanPham = dap.DaReader["MaSanPham"].ToString();
                        objSP.TenSanPham = dap.DaReader["TenSanPham"].ToString();
                        objSP.ThanhPhan = dap.DaReader["ThanhPhan"].ToString();
                        objSP.CongDung = dap.DaReader["CongDung"].ToString();
                        objSP.CachDung = dap.DaReader["CachDung"].ToString();
                        objSP.MaLoaiSanPham = dap.DaReader["MaLoaiSanPham"].ToString();
                        objSP.MaNhaSanXuat = dap.DaReader["MaNhaSanXuat"].ToString();
                        objSP.NgayTao = Convert.ToDateTime(dap.DaReader["NgayTao"]);
                        objSP.NgaySua = Convert.ToDateTime(dap.DaReader["NgaySua"]);
                        objSP.GiaBan = Convert.ToDouble(dap.DaReader["GiaBan"]);
                        objSP.SoLuotXem = Convert.ToInt32(dap.DaReader["SoLuotXem"]);
                        objSP.KhuyenMai = dap.DaReader["KhuyenMai"].ToString();
                        objSP.GiaDaGiam = Convert.ToDouble(dap.DaReader["GiaDaGiam"]);
                        objSP.IsNew = Convert.ToBoolean(dap.DaReader["IsNew"]);
                        objSP.CoHang = Convert.ToBoolean(dap.DaReader["CoHang"]);
                        objSP.DanhGia = Convert.ToInt32(dap.DaReader["DanhGia"]);

                        objTemp.ObjSanPham = objSP;

                        HinhAnh objHinh = daoHH.getByKey(objSP.MaSanPham);
                        objTemp.DuongDanAnh = (objHinh == null ? "" : objHinh.DuongDan);
                        
                        data.Add(objTemp);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return data;
        }

        public List<SanPhamXem> getListByKey()
        {
            return getListBy("", " [MaSanPham] asc");
        }
    }
}